<template>
	<view class="baseForm">
		<!-- 新增未完成！！！！！！！！！！！！！！！！！！ -->
		<u--form labelPosition="left" :model="umodel" :rules="rules" ref="uForm" class="form" labelWidth="160rpx"
			errorType="toast">
			<u-form-item label="物料条码" prop="materialBarcode">
				<u--input v-model="umodel.materialBarcode" inputAlign="right" clearable border="surround"
					:disabled="true"></u--input>
			</u-form-item>
			<u-form-item label="存货编码">
				<u--input v-model="umodel.materialCode" inputAlign="right" clearable border="surround"
					:disabled="true"></u--input>
			</u-form-item>
			<u-form-item label="物料名称">
				<u--input v-model="umodel.materialName" inputAlign="right" clearable border="surround"
					:disabled="true"></u--input>
			</u-form-item>
			<u-form-item label="物料数量">
				<u--input v-model="umodel.materialQuantity" inputAlign="right" clearable border="surround"
					:disabled="true"></u--input>
			</u-form-item>
			<u-form-item label="退货数量" prop="returnQuantity">
				<u--input v-model="umodel.returnQuantity" placeholder="请填写退货数量" inputAlign="right" clearable
					border="surround"></u--input>
			</u-form-item>
			<u-form-item label="存储地点" prop="storageLocationName">
				<u--input v-model="umodel.storageLocationName" inputAlign="right" placeholder="请选择存储地点" clearable
					border="surround" :disabled="true" disabledColor="#ffffff" @click="storageLocationShow = true">
				</u--input>
			</u-form-item>
		</u--form>
		<u-picker :show="storageLocationShow" :columns="storageLocationList" @cancel="storageLocationShow = false"
			@confirm="handelStorageLocation" keyName="storageLocationName"></u-picker>
		<!-- 提交按钮 -->
		<button type="primary" @click="submit" class="submit_btn" :loading="posting">
			提 交
		</button>
	</view>
</template>

<script lang="ts" setup>
	import { http } from "../../../utils/http";
	import { onLoad } from "@dcloudio/uni-app";
	import { computed, reactive, ref } from "vue";
	// 表单数据定义
	const umodel = ref({
		id: "",
		producingStockId: "",
		workCenterId: "",
		workCenterCode: "",
		workCenterName: "",
		materialId: "",
		materialCode: "",
		materialName: "",
		returnQuantity: "",
		materialQuantity: "",
		arriveBarcode: "",
		quantityUnit: "",
		materialBarcode: "",
		returnBarcode: "",
		storageLocationId: "",
		storageLocationCode: "",
		storageLocationName: "",
	});

	const props = defineProps({
		result: {
			type: String,
			default: "",
		},
	});
	umodel.value = JSON.parse(props.result);
	getReturnQuantity()
	async function getReturnQuantity() {
		await http({
			method: "POST",
			url: `/mes/mesmaterialreturn/returnQuantity`,
			data: {
				materialBarcode: umodel.value.materialBarcode,
				stockFrom: 0
			}
		}).then((res : any) => {
			if (res.data <= 0) {
				uni.$u.toast('退货数量大于等于库存数量！');
			}
			umodel.value.materialQuantity = res.data
		});
	}
	// 表单规则定义
	const rules = ref({
		returnQuantity: [
			{
				type: "number",
				message: "请输入数字",
				trigger: ["blur", "change"],
			},
			{
				validator: (rule, value, callback : (a ?: any) => {}) => {
					if (value) {
						if (/(^[1-9]\d*$)/.test(value)) {
							//验证正整数和0的正则
							return true;
						} else {
							return false;
						}
					} else {
						return false;
					}
				},
				message: "请输入大于0数字",
			},
		],
		storageLocationName: [
			{
				required: true,
				type: "string",
				message: "请选择存储地点",
				trigger: ["blur", "change"],
			},
		],
	});

	// 校验提交
	const uForm = ref();
	const posting = ref(false);
	async function submit() {
		uForm.value
			.validate()
			.then(async (res) => {
				console.log("传输数据", umodel);
				posting.value = true;
				if (
					Number(umodel.value.materialQuantity) -
					Number(umodel.value.returnQuantity) <
					0
				) {
					uni.$u.toast("退货数量不能超出物料数量！");
					posting.value = false;
					return;
				}
				umodel.value.producingStockId = umodel.value.id;
				umodel.value.returnBarcode = umodel.value.materialBarcode;
				umodel.value.id = "";
				// 请求
				await http({
					method: "POST",
					url: "/mes/mesmaterialreturn/save",
					data: {
						...umodel.value,
					},
				}).then((res) => {
					if (res.code == "200") {
						uni.$u.toast("物料退货成功");
						uni.redirectTo({
							url: "/pages/produce/materialReturn/index",
						});
						posting.value = false;
					}
				});
			})
			.catch((errors) => {
				console.log(errors);
			});
	}

	const storageLocationShow = ref(false);
	const storageLocationList = reactive([]);
	const getStorageLocationList = async () => {
		const { plantId } = JSON.parse(props.result);
		const { data } = await http({
			method: "GET",
			url: `/wms/storagelocation/getList?plantId=${plantId}`,
		});
		storageLocationList[0] = data;
		console.log(storageLocationList, plantId);
	};
	const handelStorageLocation = (e) => {
		let item = e.value[0];
		umodel.value.storageLocationId = item.id;
		umodel.value.storageLocationCode = item.storageLocationCode;
		umodel.value.storageLocationName = item.storageLocationName;
		console.log(umodel.value);
		storageLocationShow.value = false;
	};

	onLoad(() => {
		getStorageLocationList();
	});
</script>

<style lang="scss">
	.baseForm {
		.form {
			padding: $uni-page-padding;
		}

		.u-form-item {
			min-height: 46px;
		}

		:deep(.uicon-arrow-right) {
			top: 1px;
			margin-left: 3px;
		}

		.submit_btn {
			margin: 60rpx;
			border-radius: 60rpx;
		}

		:deep(.u-form-item__body__right__content__slot) {
			flex-direction: row-reverse;
		}

		:deep(.u-textarea__field) {
			text-align: right;
		}

		.u-textarea {
			border: 1px solid $uni-input-border-color;
			border-radius: 8px;
		}
	}
</style>